﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;

namespace myChannel9
{
    public partial class Video : PhoneApplicationPage
    {
        VideoObject video = new VideoObject();
        bool playing = false;

        public Video()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            OrientationChanged += new EventHandler<OrientationChangedEventArgs>(Video_OrientationChanged);

            if (IsolatedStorageSettings.ApplicationSettings.Contains("Video"))
            {
                video = IsolatedStorageSettings.ApplicationSettings["Video"] as VideoObject;
            }

            if (video != null)
            {
                media.Loaded += new RoutedEventHandler(media_Loaded);
                media.CurrentStateChanged += new RoutedEventHandler(media_Loaded);
                media.Source = new Uri(video.getVideo);
                play();
                showCorrectFavoriteIcon();
            }
            else
            {
                //NavigationService.GoBack();
            }
        }

        void media_Loaded(object sender, RoutedEventArgs e)
        {
            lblLoading.Visibility = Visibility.Collapsed;
        }

        void Video_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            orientationChanged();
        }

        private void orientationChanged()
        {
            if (Orientation == PageOrientation.Landscape || Orientation == PageOrientation.LandscapeLeft || Orientation == PageOrientation.LandscapeRight)
            {
                mediaControl.Width = 70;
                media.Margin = new Thickness(70, 0, 0, 0);

                btnFavorite.Visibility = Visibility.Collapsed;
                btnPlay.Visibility = Visibility.Collapsed;
            }
            else
            {
                mediaControl.Width = 0;
                media.Margin = new Thickness(0, 0, 0, 0);

                btnFavorite.Visibility = Visibility.Visible;
                btnPlay.Visibility = Visibility.Visible;
            }

            showCorrectFavoriteIcon();
        }

        private void btnFavorite_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            List<VideoObject> channel9List = null;

            if (IsolatedStorageSettings.ApplicationSettings.Contains("channel9List"))
            {
                channel9List = IsolatedStorageSettings.ApplicationSettings["channel9List"] as List<VideoObject>;

                if (channel9List != null)
                {
                    if(video.Favorite)
                    {
                        channel9List.Remove(video);
                        video.Favorite = false;
                        channel9List.Add(video);
                    }
                    else
                    {
                        channel9List.Remove(video);
                        video.Favorite = true;
                        channel9List.Add(video);
                    }

                    showCorrectFavoriteIcon();
                }

                IsolatedStorageSettings.ApplicationSettings["channel9List"] = channel9List;
            }
        }

        private void showCorrectFavoriteIcon()
        {
            string imageName = "";

            if(video.Favorite)
            {
                imageName = "Images/imgStar3.png";
            }
            else
            {
                imageName = "Images/imgStar.png";
            }

            Uri uri = new Uri(imageName, UriKind.Relative);
            ImageSource imgSource = new BitmapImage(uri);
            btnFavorite.Source = imgSource;
            btnFavorite2.Source = imgSource;
        }

        private void btnPause_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            media.Pause();
        }

        private void btnPlay_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            play();
        }

        private void play()
        {
            if (playing)
            {
                Uri uri = new Uri("Images/imgPlay.png", UriKind.Relative);
                ImageSource imgSource = new BitmapImage(uri);
                btnPlay.Source = imgSource;
                btnPlay2.Source = imgSource;

                media.Pause();
                playing = false;
            }
            else
            {
                Uri uri = new Uri("Images/imgPause.png", UriKind.Relative);
                ImageSource imgSource = new BitmapImage(uri);
                btnPlay.Source = imgSource;
                btnPlay2.Source = imgSource;

                media.Play();
                playing = true;
            }
        }
    }
}